
cc.Class({
    extends: cc.Component,

    properties:()=>({

        gameName:cc.Node,

        GameCtl:{
            default:null,
            type:require("GameControl"),
        },

        GameingNode:{
            default:null,
            type:cc.Node,
        },
        ReadyNode:{
            default:null,
            type:cc.Node,
        },
        GameOverNode:{
            default:null,
            type:cc.Node,
        },
        ScoreLabel:{
            default:null,
            type:cc.Label,
        },

        optionsPage:cc.Node,
        rankListPage:cc.Node,
        rankListItems:cc.Node,



    }),

    // LIFE-CYCLE CALLBACKS:

    onLoad () {

        //cc.error("UManager Onload");


        this.RestGame();

    },

    RestGame(){
        this.gameName.active =true;
        this.ReadyNode.active =true;
        this.GameingNode.active =false;
        this.GameOverNode.active  =false;
        this.GameCtl.RestGame();
    },


    StartGame(){
        this.gameName.active =false;
        this.ReadyNode.active =false;
        this.GameingNode.active =true;
        this.GameOverNode.active  =false;

        this.GameCtl.StartGame();
    },

    GameOver(){
        this.ReadyNode.active =false;
        this.GameingNode.active =false;
        this.GameOverNode.active  =true;
        this.ScoreLabel.string ="Score:"+this.GameCtl.scoreCtl.score;
        this.SetScoreToServer(this.GameCtl.scoreCtl.score);

    },

    ShowOptions(){
        this.optionsPage.active =true;
    },

    CloseOptions(){
        this.optionsPage.active =false;
    },

    ShowRankList(){
        this.rankListPage.active =true;

        this.GetRankList();



    },

    CloseRankList(){
        this.rankListPage.active =false;
    },




    start () {

        // for(let i=0;i<10;i++){
        //     this.rankListItems.children[i].getComponent(cc.Label).string ="第"+i+"名 "+"分数："+i;
        // }

    },

    //设置分数
    SetScoreToServer(value){

        window.h5api.submitScore(value,this.callbackSubmit)
    },

    callbackSubmit(obj){
        console.log('代码:' + obj.code + ',消息:' + obj.message + ',数据:' + obj.data)
        if(obj.code === 10000){
            console.log('上传成功')
        } else {
            console.log('上传失败')
        }
    },



    //得到rank
    GetRankList(){

        var self =this;
        window.h5api.getRank(function (obj) {
            console.log('代码:' + obj.code + ',消息:' + obj.message + ',数据:' + obj.data)
            if(obj.code === 10000){
                console.log('获取成功');
                let data = obj.data;
                for(let i = 0; i < data.length; i++){
                    if(i===10)
                    {
                        return;
                    }
                    console.log('积分:' + data[i].score + ',排名:' + data[i].rank);
                    self.rankListItems.children[i].getComponent(cc.Label).string ="第"+data[i].rank+"名 "+"分数："+data[i].score;
                }
            } else{
                console.log('获取失败')
            }
        });
    },
    callbackRank(obj){

        console.log('代码:' + obj.code + ',消息:' + obj.message + ',数据:' + obj.data)
        if(obj.code === 10000){
            console.log('获取成功');
            let data = obj.data;
            for(let i = 0; i < data.length; i++){
                if(i===10)
                {
                    return;
                }
                console.log('积分:' + data[i].score + ',排名:' + data[i].rank);
                this.rankListItems.children[i].getComponent(cc.Label).string ="第"+data[i].rank+"名 "+"分数："+data[i].score;
            }
        } else{
            console.log('获取失败')
        }

    },

});
